<?php
    require_once('init.php');
    if(Tools::getValue('login') && Tools::getValue('mdp') && Tools::getValue('prenom') && 
        Tools::getValue('nom') && Tools::getValue('email') && Tools::getValue('cgv') && 
        Tools::getValue('mdp') == Tools::getValue('confirm_mdp')) {
        $civilite = Civilite::rechercheCivilite(Tools::getValue('id_civilite'));
        $profil = Profil::rechercheProfil(1);
        
        $client = new Client(0, $civilite, $profil, Tools::getValue('login'), Tools::encrypt(Tools::getValue('mdp')), 
                            Tools::getValue('nom'), Tools::getValue('prenom'), Tools::getValue('telephone'), 
                            Tools::getValue('mobile'), Tools::getValue('email'), Tools::getValue('date_naissance'), 
                            new DateTime(), new DateTime(), 1);
        $client->add();
        if($client->getId() !== 0) {
            $_SESSION['client'] = $client;
            Tools::redirect('index.php');
        }
        else {
            echo 'L\'identifiant est déja pris';
        }
        
    }
    else if(Tools::getValue('envoie')) {
        echo    'Veuillez remplir tout les champs obligatoires';
    }
    require('header.php');
    
    $civilites = Civilite::getCivilites();
?>
<form class="form_inscription" method="POST" action="inscription.php" >
    <div class="block_milieu" id="bloc_inscription">
        <div class="label_inscription_titre">
            Veuillez vous inscrire
        </div>
        <label class="label_inscription">
            Civilité
        </label>
        <label class="label_valeur">
            <select name="id_civilite">
<?php
    foreach($civilites as $civilite) {
        echo    '<option value="'.$civilite->getId().'">'.$civilite.'</option>';
    }
?>
            </select>
        </label>
        <label class="label_inscription">
            Identifiant
        </label>
        <label class="label_valeur">
            <input type="text" name="login" size="30" maxlength="50"/><span class="etoile_inscription">*</span>
        </label>  
        <label class="label_inscription">
            Mot de passe
        </label>
        <label class="label_valeur">
            <input type="password" name="mdp" size="30" maxlength="200" /><span class="etoile_inscription">*</span>
        </label>
        
        <label class="label_inscription">
            Confirmer le mot de passe
        </label>
        <label class="label_valeur">
            <input type="password" name="confirm_mdp" size="30" maxlength="200" /><span class="etoile_inscription">*</span>
        </label>
        
        <label class="label_inscription">
            Prénom
        </label>
        <label class="label_valeur">
            <input type="text" name="prenom" size="30" maxlength="100" /><span class="etoile_inscription">*</span>
        </label>
        
        <label class="label_inscription">
            Nom
        </label>
        <label class="label_valeur">
            <input type="text" name="nom" size="30" maxlength="100" /><span class="etoile_inscription">*</span>
        </label>
        
        <label class="label_inscription">
            N° de téléphone
        </label>
        <label class="label_valeur">
            <input type="text" name="telephone" size="14" maxlength="14" data-format="tel" />
        </label>
        
        <label class="label_inscription">
            N° de mobile
        </label>
        <label class="label_valeur">
            <input type="text" name="mobile" size="14" maxlength="14" data-format="tel"/>
        </label>
        
        <label class="label_inscription">
            E-mail
        </label>
        <label class="label_valeur">
            <input type="text" name="email" size="30" maxlength="150" data-format="email"/><span class="etoile_inscription">*</span>
        </label>
        
        <label class="label_inscription">
            Date de naissance
        </label>
        <label class="label_valeur">
            <input type="text" placeholder="dd/mm/YYYY" name="date_naissance" size="12" maxlength="10" 
                data-format="date"/><span class="etoile_inscription">*</span>
        </label>
        
        <div class="inscription_cgv">
            <input type="checkbox" name="cgv" id="checkbox_inscription_cgv" value="1" />
            
                En cochant cette case, je reconnais avoir pris connaissance des
                conditions générales de ventes (CGV)<span class="etoile_inscription">*</span><br>  
                
                <p style="color: red;">* champs obligatoires</p>
          
        </div>
            
        <label class="label_inscription">
            &nbsp;
        </label>
        <label class="label_valeur">
            <input type="submit" value="Valider" name="envoie"/>
        </label>
    </div>
</form>

<?php
    require('footer.php');
?>
